﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace TheOliver.Controls
{
    public class PageTurnerPage : Panel
    {
        PageTurnerHalfPage _leftPage;
        PageTurnerHalfPage _rightPage;

        public PageTurnerPage()
        {
            _leftPage = new PageTurnerHalfPage();
            _rightPage = new PageTurnerHalfPage();

            this.Loaded += (s, e) =>
                {
                    CreatePages();

                };
        }

        // TODO: Size bestimmen

        FrameworkElement _content;

        //protected override Size ArrangeOverride(Size finalSize)
        //{
        //    CreatePages();

        //    return base.ArrangeOverride(finalSize);
        //}

        private void CreatePages()
        {

            if (this.Children.Count == 0)
            {
                return;
            }

            _content = this.Children[0] as FrameworkElement;
            _content.VerticalAlignment = System.Windows.VerticalAlignment.Stretch;
            _content.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;

            WriteableBitmap wb = new WriteableBitmap(_content, new MatrixTransform());

            ImageBrush ib = new ImageBrush();
            ib.ImageSource = wb;

            Image leftImage = new Image();
            leftImage.Source = wb;

            Image rightImage = new Image();
            rightImage.Source = wb;

            _rightPage.Content = rightImage;
            _leftPage.Content = leftImage;

            this.Children.Add(_leftPage);
            this.Children.Add(_rightPage);
        }
    }
}
